package service.impl;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.UsersDao;
import entity.Users;
import service.UsersService;

@Service
public class UsersServiceImpl implements UsersService {

	// 注解的方式注入SqlSessionFactory
	@Autowired
	SqlSessionFactory factory;

	// 根据账号密码查找账户
	@Override
	public boolean find(String username, String password) {
		SqlSession sqlSession = null;
		try {
			sqlSession = factory.openSession();
			UsersDao dao = sqlSession.getMapper(UsersDao.class); // 动态代理生成对象
			int i = dao.find(username, password);
			if (i > 0) {
				return true;
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			sqlSession.close();
		}
		return false;
	}

	// 注册用户
	@Override
	public boolean add(String username, String password) {
		SqlSession sqlSession = null;
		try {
			sqlSession = factory.openSession();
			UsersDao dao = sqlSession.getMapper(UsersDao.class);
			Users user = dao.findByUserName(username);
			if (user==null) {
				int i = dao.add(username, password);
				if (i > 0) {
					return true;
				}
				sqlSession.commit();
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			sqlSession.close();
		}
		return false;
	}
	//根据用户账号查找账号信息
	@Override
	public Users findByUserName(String username) {
		SqlSession sqlSession = null;
		Users users = null;
		try {
			sqlSession = factory.openSession();
			UsersDao dao = sqlSession.getMapper(UsersDao.class);
			users = dao.findByUserName(username);
			return users;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			sqlSession.close();
		}
		return null;
	}

	//更新个人信息地址的方法
	@Override
	public int updateaddress(String address,String username) {
		SqlSession sqlSession = null;
		try {
			sqlSession = factory.openSession();
			UsersDao dao = sqlSession.getMapper(UsersDao.class);
			int i = dao.updateaddress(address, username);
			sqlSession.commit();
			return i;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			sqlSession.close();
		}
		return 0;
	}

	//更新个人信息生日的方法
	@Override
	public int updatebirthday(String birthday,String username) {
		SqlSession sqlSession = null;
		try {
			sqlSession = factory.openSession();
			UsersDao dao = sqlSession.getMapper(UsersDao.class);
			int i = dao.updatebirthday(birthday, username);
			sqlSession.commit();
			return i;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			sqlSession.close();
		}
		return 0;
	}

	//更新个人简介的方法
	@Override
	public int updateintro(String intro,String username) {
		SqlSession sqlSession = null;
		try {
			sqlSession = factory.openSession();
			UsersDao dao = sqlSession.getMapper(UsersDao.class);
			int i = dao.updateintro(intro, username);
			sqlSession.commit();
			return i ;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			sqlSession.close();
		}
		return 0;
	}
	
	public Users findByUserId(int userId) {
		SqlSession sqlSession=null;
		Users users=null;
		try {
			sqlSession=factory.openSession();
			UsersDao dao = sqlSession.getMapper(UsersDao.class);
			users = dao.findByUserId(userId);
			return users;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}  finally {
			sqlSession.close();
		}
		return null;
	}

	@Override
	public boolean updatepassword(String password, String username) {
		SqlSession sqlSession = null;
		try {
			sqlSession = factory.openSession();
			UsersDao dao = sqlSession.getMapper(UsersDao.class);
			dao.updatepassword(password, username);
			sqlSession.commit();
				return true;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			sqlSession.close();
		}
		return false;
	}

	@Override
	public boolean uploadheadicon(String picUrl, String username) {
		SqlSession sqlSession = null;
		try {
			sqlSession = factory.openSession();
			UsersDao dao = sqlSession.getMapper(UsersDao.class);
			dao.uploadheadicon(picUrl, username);
			sqlSession.commit();
			return true;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			sqlSession.close();
		}
		return false;
	}

	@Override
	public List<Users> findByTopPoint() {
		SqlSession sqlSession = null;
		try {
			sqlSession = factory.openSession();
			UsersDao dao = sqlSession.getMapper(UsersDao.class);
			List<Users> list = dao.findByTopPoint();
			return list;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			sqlSession.close();
		}
		return null;
	}

	@Override
	public int updateNickname(String nickname, String username) {
		SqlSession sqlSession = null;
		try {
			sqlSession = factory.openSession();
			UsersDao dao = sqlSession.getMapper(UsersDao.class);
			int result = dao.updateNickname(nickname, username);
			return result;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			sqlSession.close();
		}
		return 0;
	}
	
	
}
